package com.dx.financehy.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dx.financehy.exception.BadRequestException;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.prefs.BackingStoreException;

/**
 * <pre>
 * @描述：
 * @作者：CLD
 * @创建时间：2020/10/26 19:35
 * </pre>
 **/
public class JsonUtils {

    private JsonUtils() {
    }

    public static String toJson(Object object) {
        return JSON.toJSONString(object, SerializerFeature.WriteMapNullValue,
                SerializerFeature.DisableCircularReferenceDetect);
    }

    public static Map<?, ?> toMap(String json) throws BadRequestException {
        try {
            return JSON.parseObject(json, Map.class);
        } catch (Exception e) {
            throw new BadRequestException("Josn解析失败:" + e.getMessage());
        }
    }

    public static List<?> toList(String json) throws BadRequestException {
        try {
            return JSON.parseObject(json, List.class);
        } catch (Exception e) {
            throw new BadRequestException("Josn解析失败:" + e.getMessage());
        }
    }

    public static List<?> toList(String json, Class<?> c) throws BadRequestException {
        try {
            return JSON.parseArray(json, c);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BadRequestException("Josn解析失败:" + e.getMessage());
        }
    }

    public static <T> T toObject(String json, Class<T> c) throws BadRequestException {
        try {
            return JSON.parseObject(json, c);
        } catch (Exception e) {
            throw new BadRequestException("Josn解析失败:" + e.getMessage());
        }
    }

    public static <T> T toObject(String json, Class<T> c, Feature... feature) throws BadRequestException {
        try {
            return JSON.parseObject(json, c, feature);
        } catch (Exception e) {
            throw new BadRequestException("Josn解析失败:" + e.getMessage());
        }
    }

    public static JSONObject toObject(String json) throws BadRequestException {
        try {
            return JSON.parseObject(json);
        } catch (Exception e) {
            throw new BadRequestException("Josn解析失败:" + e.getMessage());
        }
    }

    /**
     * 接口列表返回，转对应对象工具方法
     * @param resultStr   接口返回
     * @param targetClass 转目标对象
     * @param message     如果有报错的话自定义异常说明
     * @return
     * @param <T>
     */
    public static <T> List<T> Result2List(String resultStr, Class<T> targetClass, String message) throws BackingStoreException {
        List<T> targetList = new ArrayList<>();
        if (resultStr != null) {
            JsonParser jp = new JsonParser();
            //将json字符串转化成json对象
            JsonObject jo = jp.parse(resultStr).getAsJsonObject();
            Integer code = jo.get("code").getAsInt();
            if (code == 0) {
                JsonArray jsonArray = jo.getAsJsonObject("data").getAsJsonArray("list");
                String js = jsonArray.toString();
                targetList = JSONObject.parseArray(js, targetClass);
            }else {
                if (StringUtils.isNotBlank(message)){
                    throw new BackingStoreException(message);
                }else {
                    throw new BackingStoreException(jo.get("msg").getAsString());
                }
            }
        }
        return targetList;
    }
}
